<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        .web {
            padding: 10px;
            display: flex;
        }
        aside {
            width: 200px;
            height: 100vh;
            display: flex;
            align-items: center;
            position: fixed;
        }
        aside ul {
            width: 100%;
        }
        aside li {
            line-height: 36px;
            padding-left: 20px;
            border-bottom: 1px solid #ccc;
            cursor: pointer;
        }
        .active {
            border-bottom: 2px solid #f80909;
            color: #f80909;
        }
        main {
            flex: 1;
            padding: 0 10px;
            padding-left: 210px;
        }
        .main-box {
            width: 100%;
            height: 500px;
            font-size: 20px;
            padding: 0 10px;
        }
        .main-box:nth-child(odd) {
            background-color: skyblue;
            color: #fff;
        }
        .main-box:nth-child(even) {
            background-color: pink;
        }
        
    </style>
</head>
<body>
    <div class="web">
        <aside>
            <ul>
                <li>男装</li>
                <li>女装</li>
                <li>童装</li>
                <li>电子产品</li>
                <li>电影</li>
                <li>美食</li>
            </ul>
        </aside>
        <main>
            <div class="main-box menswear">男装</div>
            <div class="main-box women">女装</div>
            <div class="main-box children">童装</div>
            <div class="main-box product">电子产品</div>
            <div class="main-box movie">电影</div>
            <div class="main-box food">美食</div>
        </main>
    </div>
    <script>
        let web = document.querySelector('.web')
        let allLi = web.querySelectorAll("aside li")
        let allMain = document.querySelectorAll(".main-box")

        // 点击滚动
        let clickScrollTop = 0
        allLi.forEach((el,index) => {
            el.addEventListener("click", function(){
                document.querySelector("aside .active")?.classList.remove("active")
                this.classList.add("active")
                // 设置滚动效果
                clickScrollTop = allMain[index].offsetTop
                document.documentElement.scrollTo({
                    top: clickScrollTop,
                    // 添加behavior值为smooth 实现平滑滚动效果
                    behavior: "smooth"
                })
            })
        });
        // 页面滚动 左侧导航自动高亮
        window.addEventListener("scroll", function(){
            const scrollTop = getScroll().top
            for(var i=0; i<allMain.length; i++){
                if(scrollTop>=allMain[i].offsetTop-100){
                    document.querySelector("aside .active")?.classList.remove("active")
                    allLi[i].classList.add("active")
                }
            }
        })

        // 获取滚动条距离顶部的位置
        function getScroll() {
            return {
            left: window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0,
            top: window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0
            };
        }

    </script>
</body>
</html>